package com.eu.ebpm.engine.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

public class XmlUtil {

	@SuppressWarnings("unchecked")
	public static void delAllElements(Element root) {
		List<Element> elements = root.elements();
		for(int i=0; i<elements.size(); i++) {
			Element e = elements.get(i);
			root.remove(e);
		}
	}
	
	public static Document getDocument(File file) {
		Document document = null;
		try {
			SAXReader reader = new SAXReader(); 
			reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
			document = reader.read(file);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static Document getDocument(String fileName) {
		Document document = null;
		try {
			SAXReader reader = new SAXReader(); 
			reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
			document = reader.read(new File(fileName));
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static Document getDocument(Reader in) {
		Document document = null;
		try {
			SAXReader reader = new SAXReader(); 
			reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
			document = reader.read(in);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	@SuppressWarnings("unchecked")
	/**
	 * xPath="/struts/package/action[@name='Menu']"
	 */
	public static List<Element> getElements(String xPath, Document document) {
		return document.selectNodes(xPath);
	}
	
	@SuppressWarnings("unchecked")
	public static List<Element> getElements(String xPath, Element element) {
		return element.selectNodes(xPath);
	}
	
	@SuppressWarnings("unchecked")
	public static Element getElement(String xPath, Document document) {
		List<Element> elements = document.selectNodes(xPath);
		if(elements.size() != 0)
			return elements.get(0);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public static Element getElement(String xPath, Element element) {
		List<Element> elements = element.selectNodes(xPath);
		if(elements.size() != 0)
			return elements.get(0);
		return null;
	}
	
	public static void remove(Element element) {
		element.getParent().remove(element);
	}
	
	public static void createFile(Document document, File file) {
		XMLWriter output = null;
		try {
			if(!file.exists()) file.createNewFile();
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			output = new XMLWriter(new FileOutputStream(file), format);
			output.write(document);
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void createFile(Document document, String fileName) {
		XMLWriter output = null;
		try {
			File file = new File(fileName);
			if(!file.exists()) file.createNewFile();
			OutputFormat format = OutputFormat.createPrettyPrint();  
			format.setEncoding("UTF-8");
			output = new XMLWriter(new FileOutputStream(file), format);
			output.write(document);
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String[] args) throws Exception {
//		String str = "D:/workplace/platform/001/WebRoot/xml/services.xml";
//		String teststr = "C:/Users/Administrator/Desktop/newWeb/test.xml";
//		File modelFile = new File(str);
//		File test = new File(teststr);
//		Document document = XmlUtil.getDocument(modelFile);
//		String image = "/data/item[@name='1']/image";
//		String text = "/data/item[@name='1']/text";
//		
//		Element e = XmlUtil.getElement(image, document);
//		Element etext = XmlUtil.getElement(text, document);
//		e.setText("asdflksdjflskd.jpg");
//		etext.clearContent();
//		etext.addCDATA("<!-- BEGIN --><normal_text>aasdfsdfasdf.jpg<a href=\"asdfsd.jsp\">haha</a> </normal_text><!-- END -->");
//		
//		XmlUtil.createFile(document, test);
		
//		reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
		SAXReader reader = new SAXReader(); 
		StringReader sr = new StringReader("<foreach collection=\"columns\" item=\"column\" separator=\",\" open=\"(\" close=\")\" index=\"index\" >${column}</foreach>");
		Document document = reader.read(sr);
		Element root = document.getRootElement();
		System.out.println(root.attributeValue("collection"));
		System.out.println(root.attributeValue("item"));
		System.out.println(root.attributeValue("separator"));
		System.out.println(root.attributeValue("open"));
		System.out.println(root.attributeValue("close"));
		System.out.println(root.attributeValue("index"));
		System.out.println(document.getRootElement().getText());
		
	}
	
	
	
	
	
	
	
	
	
}
